<?php
namespace Admin\Controller;
class ClassifyController extends CheckController {
    public function index(){
        $this->page_title="سەھىپىلەر";
        $classifys=_sort(M("cate")->order("sort desc")->limit(0,400)->select());
        $this->assign('classifys',$classifys);
        $this->display();
    }
    //用ajax分类状态
    public function sortStatus(){
        $id=I('post.id');
        $post['status']=I('post.status');
        if(!M('cate')->where("id='$id'")->save($post)){
            $data=array("status"=>0,"content"=>"مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"content"=>"مۇۋاپقىيەتلىك بولدى !");
        }
         $this->ajaxReturn($data);
    }
    //分配页面
    public function add(){
        $this->page_title="سەھىپە قوشۇش";
        $classify=_sort(M("cate")->select());
        $this->assign('classify',$classify);
        $this->display("add");
    }
    //接收数据并且写入到数据库
    public function addhend(){
        if(stripos(trim(I('post.name')),"،")){
            $post_data['names']=explode("،",I('post.name'));
        }elseif (stripos(trim(I('post.name')),",")){
            $post_data['names']=explode(",",I('post.name'));
        }elseif (stripos(trim(I('post.name')),"  ")){
            $post_data['names']=explode("  ",I('post.name'));
        }elseif(stripos(trim(I('post.name'))," ")){
            $post_data['names']=I('post.name');
        }elseif(is_numeric(trim(I('post.name')))){
            $post_data['names']=I('post.name');
        }
        $post_data['sort']=I('post.sort');
        $post_data['pid']=I('post.pid');
        $post_data['status']=I('post.status');
        $rules = array(
            array('name','require','سەھىپە نامىنى چۇقۇم يېزىڭ！',1), //默认情况下用正则进行验证
            array('pid','require','ماس سەھىپە تاللاڭ',2)
        );
        if(!M("cate")->validate($rules)->create()){
            $this->error(M("cate")->getError());
        }else{
            $i=M("cate")->max('sort');
            if(is_array($post_data['names'])){
                foreach ($post_data['names'] as $name){
                    $name=htmlspecialchars(trim($name));
                    $i++;
                    $data=array(
                        'name'=>$name,
                        'sort'=>I('post.sort')+$i,
                        'pid'=>I('post.pid'),
                        'status'=>I('post.status')
                    );
                    $result=M("cate")->add($data);
                }
            }else{
                $result=M("cate")->add(I('post.'));
            }
            if($result){
                $this->display("../index");
                _check_alert("مۇۋاپىقىيەتلىك بولدى !","index.html",1);
            }
        }
    }


    //编辑分类
    public function edit(){
        $id=I('get.id');
        $this->page_title="سەھىپە تەھرىرلەش";
        //获取要编辑的一条数据
        $classifys=M("cate")->find($id);
        $this->assign("classifys",$classifys);
        //获取所有的分类
        $classify=_sort(M("cate")->select());
        $this->assign('classify',$classify);
        $this->display("edit");
    }
    //接收数据并且保存到数据库
    public function savehend(){
        $rules = array(
            array('name','require','سەھىپە نامىنى چۇقۇم يېزىڭ！',1), //默认情况下用正则进行验证
            array('pid','require','ماس سەھىپە تاللاڭ',2)
        );
        $User=M("cate");
        if(!$User->validate($rules)->create()){
            $this->error($User->geterror(),"",1);
        }else{
            $result =$User->save(); // 保存到数据库
            if($result!==false){
                $this->display("../index");
                _check_alert("ئۇچۇر ئۆزگەرتىش مۇۋاپىقيەتلىك بولدى !","index.html",1);
            }
        }
    }

    //用ajax删除分类
    public function del(){
        $id=I('post.id');
        $ret = M("cate")->where(array('pid'=>$id))->getField('id',true);
        //如果有子分传给deleteallclass( )函数
        if(is_array($ret)){
            $this->deleteallclass($ret);
        }
        //获取要编辑的一条数据
        $data=M("cate")->delete($id);
        if(!$data){
            $data=array("status"=>0,"message"=>"ئۆچۈرۈش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"message"=>"ئۆچۈرۈش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }
    //如果有子分类删除
    private function deleteallclass($ids){
        $mod = M('cate');
        $ret2=implode(',',$ids);
        $mod->delete($ret2);
    }
    //用ajax排序分类
    public function sortfen(){
        $post=array();
        $id=I('post.id');
        $post['sort']=I('post.sort');
        //获取要编辑的一条数据
        $data=M("cate")->where("id='$id'")->save($post);
        if(!$data){
            $data=array("status"=>0,"message"=>"رەتلەش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"message"=>"رەتلەش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }

}